package com.example.demo1105.demo3;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: FBS
 * @Description: TODO
 * @DateTime: 2022/11/6 20:23
 *@Version:1.0
 */
public class TestThree {
    public static void main(String[] args) {
        ArrayList<Animal> animal = new ArrayList<>();
        animal.add(new Animal("野猪佩奇",8,'F',"猫"));
        animal.add(new Animal("杀猪饲料",6,'M',"猪"));
        animal.add(new Animal("野猪乔治",19,'F',"猪"));
        animal.add(new Animal("隔壁老王",2,'M',"猫"));
        animal.add(new Animal("隔壁老李",3,'F',"鸟"));

        animal.stream().filter(item -> item.getAge()>4).forEach(item -> System.out.println("item = " + item));
        animal.stream().filter(item -> item.getAge()>4).forEach(System.out::println);

        animal.stream().map(item -> item.getName()).forEach(item -> System.out.println("item = " + item));
        animal.stream().map(item -> item.getName()).forEach(System.out::println);

        List<String> collect = animal.stream().map(item -> item.getName()).collect(Collectors.toList());
        System.out.println("collect = " + collect);

        animal.stream().sorted((o1, o2) -> o1.getAge() - o2.getAge()).forEach(System.out::println);

        Optional<Animal> first = animal.stream().findFirst();
        System.out.println("first = " + first);

        Optional<Animal> any = animal.stream().findAny();
        System.out.println("any = " + any);

        boolean b = animal.stream().anyMatch(item -> item.getAge() > 0);
        System.out.println("b = " + b);
        boolean b1 = animal.stream().anyMatch(item -> item.getAge() > 6);
        System.out.println("b1 = " + b1);

        long count = animal.stream().count();
        System.out.println("count = " + count);

        Optional<Animal> max = animal.stream().max((o1, o2) -> o1.getAge() - o2.getAge());
        System.out.println("max = " + max);

        Optional<Animal> min = animal.stream().min((o1, o2) -> o1.getAge() - o2.getAge());
        System.out.println("min = " + min);


    }
}
class Animal{
    private String name;
    private Integer age;
    private char sex;

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", kind='" + kind + '\'' +
                '}';
    }

    public Animal(String name, Integer age, char sex, String kind) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.kind = kind;
    }

    private String kind;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }
}